package com.ebt.m.proposal_v2.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ebt.m.R;

/**
 * 处理drawableLeft/drawableTop/drawableRight/drawableBottom尺寸的TextView
 *
 * @author archer.qi
 *         Created on 2017/5/9.
 */
public class CompoundTextView extends TextView {

    private Point[] compoundSizes = new Point[4];

    public CompoundTextView(Context context) {
        super(context);
    }

    public CompoundTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Drawable[] compoundDrawables = getCompoundDrawables();
        if (compoundDrawables == null || compoundDrawables.length == 0) {
            return;
        }
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.CompoundTextView);

        Drawable left = init(t, compoundDrawables[0],
                R.styleable.CompoundTextView_drawableLeftWidth,
                R.styleable.CompoundTextView_drawableLeftHeight, 0);
        Drawable top = init(t, compoundDrawables[1],
                R.styleable.CompoundTextView_drawableTopWidth,
                R.styleable.CompoundTextView_drawableTopHeight, 1);
        Drawable right = init(t, compoundDrawables[2],
                R.styleable.CompoundTextView_drawableRightWidth,
                R.styleable.CompoundTextView_drawableRightHeight, 2);
        Drawable bottom = init(t, compoundDrawables[3],
                R.styleable.CompoundTextView_drawableBottomWidth,
                R.styleable.CompoundTextView_drawableBottomHeight, 3);

        setCompoundDrawables(left, top, right, bottom);
    }

    private Drawable init(TypedArray t, Drawable drawable, int widthAttr, int heightAttr, int position) {
        if (drawable == null) {
            return null;
        }

        int width;
        int height;
        if (t == null) {
            width = drawable.getIntrinsicWidth();
            height = drawable.getIntrinsicHeight();
            setBounds(drawable, width, height);
            return drawable;
        }

        width = (int) t.getDimension(widthAttr, drawable.getIntrinsicWidth());
        height = (int) t.getDimension(heightAttr, drawable.getIntrinsicWidth());

        Point size = new Point(width, height);
        compoundSizes[position] = size;

        setBounds(drawable, width, height);
        return drawable;
    }

    private void setBounds(Drawable drawable, int width, int height) {
        if (drawable != null) {
            if (width <= 0 || height <= 0) {
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            } else {
                drawable.setBounds(0, 0, width, height);
            }
        }
    }

    public void setLeftDrawable(Drawable drawable) {
        Point size = compoundSizes[0];
        if(size == null) {
            setBounds(drawable, 0, 0);
        }else {
            setBounds(drawable, size.x, size.y);
        }
        setCompoundDrawables(drawable, null, null, null);
    }

    public void setTopDrawable(Drawable drawable) {
        Point size = compoundSizes[1];
        if(size == null) {
            setBounds(drawable, 0, 0);
        }else {
            setBounds(drawable, size.x, size.y);
        }
        setCompoundDrawables(null, drawable, null, null);
    }

    public void setRightDrawable(Drawable drawable) {
        Point size = compoundSizes[2];
        if(size == null) {
            setBounds(drawable, 0, 0);
        }else {
            setBounds(drawable, size.x, size.y);
        }
        setCompoundDrawables(null, null, drawable, null);
    }

    public void setBottomDrawable(Drawable drawable) {
        Point size = compoundSizes[3];
        if(size == null) {
            setBounds(drawable, 0, 0);
        }else {
            setBounds(drawable, size.x, size.y);
        }
        setCompoundDrawables(null, null, null, drawable);
    }

}

